//
//  RootViewController.m
//  N2ST
//
//  Created by Eduardo Oliveros on 30/12/08.
//  Copyright Eduardo Oliveros 2008. All rights reserved.
//

#import "RootViewController.h"
#import "N2STAppDelegate.h"
#import "TaskCell.h"


@implementation RootViewController
@synthesize projects;
@synthesize cellSelected;

//Insert in SmartTime a Task
-(void)insertInSTTaskWithName:(NSString*) name Duration:(NSString*) duration {
	NSMutableString* taskName = [NSMutableString stringWithString:name ];
	[taskName replaceOccurrencesOfString:@" " withString:@"§" options:NSLiteralSearch range:NSMakeRange(0, [taskName length])]; 
	NSLog(@"name=%@", taskName);
	NSLog(@"time=%@", duration );
	NSUInteger seconds = [duration integerValue]*60;
	NSMutableString* URLstr = [NSMutableString stringWithFormat:@"smarttimeplus://smarttimeplus/;%@¶NoLocation¶NoNotes¶0¶%d¶1¶0¶0000000000.000000¶0000000000.000000¶0¶-1¶NoPhones¶0000000000.000000¶1¬¬0¶NoExceptions¶0¶2.0¶0¶NoAlert¶0000000000.000000",
							   taskName, seconds];
	NSLog(@"URLstr=", URLstr);
	NSString* STURL = [URLstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSLog(@"URL=%@", STURL);
    BOOL res = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:
														   STURL]];
	NSLog(@"res=%i", res);
}

///FUNCTIONs about SELECTION of a CELL
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	NSLog(@"User Pressed Button %d\n", buttonIndex + 1);
	[actionSheet release];
	NSInteger nProject = [cellSelected section];
	NSInteger nTask = [cellSelected row];
	NSString* cellAct = [NSString stringWithFormat:@"action in (%d,%d)", nProject, nTask];
	TaskNode* taskNode = [[[projects objectAtIndex:nProject] tasks] objectAtIndex:nTask];
	NSString* name = [taskNode taskName];
	NSString* duration = [taskNode taskDuration];
	if(buttonIndex == 0){ //insert in ST
		NSLog(@"ST: %@", cellAct);
		[self insertInSTTaskWithName:name Duration:duration];
	}else if(buttonIndex == 1){ //mark as done and remove from the table
		NSLog(@"removing: %@", cellAct);
		[(N2STAppDelegate*)[UIApplication sharedApplication].delegate completeAction:nTask ofProject:nProject];
		[[[projects objectAtIndex:nProject] tasks] removeObjectAtIndex:nTask];
	    [[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:cellSelected] withRowAnimation:YES];
	}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// Navigation logic
	//muestra la alerta con los tres botones
	[self setCellSelected:indexPath];
	UIActionSheet *menu = [[UIActionSheet alloc] 
						   initWithTitle: @"Task Action" 
						   delegate:self
						   cancelButtonTitle:@"Cancel"
						   destructiveButtonTitle:nil
						   otherButtonTitles:@"Insert in ST", @"Mark as Done in Nozbe", nil];
	[menu showInView:self.tableView];	 
}


- (void)viewDidLoad {
	// Add the following line if you want the list to be editable
	// self.navigationItem.leftBarButtonItem = self.editButtonItem;
	projects = [(N2STAppDelegate*)[UIApplication sharedApplication].delegate projects];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	[self setProjects: [(N2STAppDelegate*)[UIApplication sharedApplication].delegate projects]];
	if(projects == nil){
		return 1;
	}else{
		return projects.count;
	}
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if(projects == nil){
		return 0;
	}else{
		return [[[projects objectAtIndex:section] tasks]count];
	}
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	TaskCell *cell = (TaskCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[TaskCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	
	// Set up the cell
	NSUInteger nProject = [indexPath section];
	NSUInteger nTask = [indexPath row];
	TaskNode* taskNode = [[[projects objectAtIndex:nProject] tasks] objectAtIndex:nTask];
	[cell setName: [taskNode taskName]];
	[cell setDuration:[taskNode taskDuration]];
	return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	if(projects !=nil){
		return [[projects objectAtIndex:section] projectName];
	}
	else{
		return @"-Loading tasks-";
	}
}

/*
 Override if you support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
		
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		// Delete the row from the data source
		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
	}	
	if (editingStyle == UITableViewCellEditingStyleInsert) {
		// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
	}	
}
*/


/*
 Override if you support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	// Return NO if you do not want the specified item to be editable.
	return YES;
}
*/


/*
 Override if you support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
 Override if you support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
	// Return NO if you do not want the item to be re-orderable.
	return YES;
}
 */ 


- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[super dealloc];
}


@end

